<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/17
 * Time: 14:16
 */

namespace app\meeting\controller;


use app\meeting\model\MeetArticleModel;
use app\meeting\model\MeetCategoryModel;
use app\meeting\model\MeetModel;
use cmf\controller\HomeBaseController;
use think\Db;
use think\helper\Time;

class IndexController extends HomeBaseController
{
    public function index()
    {
        $meetModel = new MeetModel();
        $now = date('Y-m-d', time());
        $meetings = $meetModel->with('provinceText')->order('start_time desc')->where('end_time', '> time', $now)->limit(8)->select();
        $this->assign('meetings', $meetings);
        $last_year = Time::lastYear();
        $old_meetings = $meetModel->with('provinceText')->order('start_time desc')->where('start_time', '> time', $last_year[1])->where('end_time', '< time', $last_year[1])->limit(6)->select();

        $this->assign('old_meetings', $old_meetings);


        return $this->fetch(":index");
    }

    public function lists()
    {
        $subject = $this->request->param('subject', '');
        $month = $this->request->param('month', '');
        $year = $this->request->param('year', '');
        $province = $this->request->param('province', '');
        $categories = MeetCategoryModel::all();
        $this->assign('categories', $categories);
        $tags = Db::name('meet_tags')->select();
        $this->assign('tags', $tags);
        $this->assign('subject', $subject);
        $this->assign('month', $month);
        $this->assign('year', $year);
        $this->assign('province', $province);
        $name = $this->request->param('name');

        $meetModel = new MeetModel();
        $where = [];
        if(!empty($subject)){
            $where['type'] = intval($subject);
        }
        if(!empty($province)){
            $where['province'] = $province;
        }
        if(!empty($year)){
            $where["YEAR(FROM_UNIXTIME(start_time,'%Y-%m-%d'))"] = $year;
        }
        if(!empty($month)){
            $where["MONTH(FROM_UNIXTIME(start_time,'%Y-%m-%d'))"] = $month;
        }
        if(!empty($name)){
            $where['name'] = ['like',"%$name%"];
            $this->assign('name',$name);
        }
        $meets = $meetModel->with('provinceText')->where($where)->paginate();
        $this->assign('meets', $meets);
        $this->assign('pages',$meets->render());
        return $this->fetch(":list");
    }

    public function info($id){
        $meetModel = new MeetModel();
        $meet = $meetModel->with('provinceText')->where('id',$id)->find();
        $type = $this->request->param('type','basic');
        $this->assign('meet',$meet);
        $this->assign('type',$type);
        switch ($type){
            case 'basic':
                $content = htmlspecialchars_decode($meet['basic_info']);
                $this->assign('content',$content);
                $title = "基本信息";
                $this->assign('title',$title);
                break;
            case 'zuzhi':
                $content = htmlspecialchars_decode($meet['zhuzhi_info']);
                $this->assign('content',$content);
                $title = "组织机构";
                $this->assign('title',$title);
                break;
            case 'tg':
                $content = htmlspecialchars_decode($meet['tg_info']);
                $this->assign('content',$content);
                $title = "征文投稿";
                $this->assign('title',$title);
                break;
            case 'register':
                $content = htmlspecialchars_decode($meet['register_info']);
                $this->assign('content',$content);
                $title = "参会注册";
                $this->assign('title',$title);
                break;
            case 'contact':
                $content = htmlspecialchars_decode($meet['contact_info']);
                $this->assign('content',$content);
                $title = "联系我们";
                $this->assign('title',$title);
                break;
        }
        return $this->fetch(":info");
    }
    public function articles($mid){
       $meetArticleModel = new MeetArticleModel();
       $articles = $meetArticleModel->where('mid',$mid)->paginate();
        $meetModel = new MeetModel();
        $meet = $meetModel->with('provinceText')->where('id',$mid)->find();
       $this->assign('articles',$articles);
        $this->assign('meet',$meet);

       return $this->fetch(":articles");
    }

    public function article($id){
        $meetArticleModel = new MeetArticleModel();
        $article = $meetArticleModel->where('id',$id)->find();
        $meetModel = new MeetModel();
        $meet = $meetModel->with('provinceText')->where('id',$article['mid'])->find();
        $this->assign('article',$article);
        $this->assign('meet',$meet);
        return $this->fetch(":article");
    }

    public function show($id)
    {
        $meetModel = new MeetModel();
       $meet = $meetModel->with('provinceText')->where('id',$id)->find();
       $this->assign('meet',$meet);
        $meetArticleModel = new MeetArticleModel();
        $articles = $meetArticleModel->where('mid',$id)->limit(8)->select();
        $this->assign('articles',$articles);
        return $this->fetch(':show');
    }

}